package com.test.web;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.test.model.EbNews;
import com.test.model.EbProduct;
import com.test.model.EbProductCategory;
import com.test.service.EbNewsService;
import com.test.service.EbProductCategoryService;
import com.test.service.EbProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller  //证——访问首页
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private EbProductService ebProductService;
    @Autowired
    private EbNewsService ebNewsService;
    @Autowired
    private EbProductCategoryService ebProductCategoryService;
    //如果浏览器没有传递页数，默认是第1页  @RequestParam(defaultValue = "1") int psize
    @RequestMapping("/front")//前台
    public String index(@RequestParam(defaultValue = "1") int psize,
                        @RequestParam(defaultValue = "8")  int pageSize,
                        Model model, HttpServletRequest request){

        //1.查分页商品数据
        Page<EbProduct> productPage = ebProductService.list(psize,pageSize);
        //2.查最新的新闻数据
        Page<EbNews>  newsPage = ebNewsService.list(psize,pageSize);
        //3.查商品分类数据
        Map<String, List<EbProductCategory>> cateMap = ebProductCategoryService.selectBigAndSmallCate();
        //4.将数据绑定到model中key-value
        model.addAttribute("productPage",productPage);//绑定分类商品数据
        model.addAttribute("newsPage",newsPage);
        //model.addAttribute("cateMap",cateMap);//绑定分类商品数据

        //将分类数据放入到session中，才能在多个请求中传递
        request.getSession().setAttribute("cateMap",cateMap);
        //5.指定视图(view)--html
        return "index";
    }
}
